/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bhatni.taskmanager;

import com.bhatni.taskmanager.task.Task;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author virendra
 */
public class XMLBeanUtils {
    private XPath xpath;
    
    public XMLBeanUtils() {
        XPathFactory xpathFactory = XPathFactory.newInstance();
        xpath = xpathFactory.newXPath();
    }

    public NodeList getChildrenbyTagName(Element element, String tagName) {
        NodeList nodes = null;
        try {
            nodes = (NodeList) xpath.evaluate(tagName, element,XPathConstants.NODESET);
        } catch (XPathExpressionException ex) {
            Logger.getLogger(XMLBeanUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return nodes;
    }
    public List<Object> createInstances(Element parent,String rootPackage, String type) {
        List objs = new ArrayList();
        
        NodeList objects;
        try {
            objects = (NodeList) xpath.evaluate(type, parent,XPathConstants.NODESET);
        } catch (XPathExpressionException ex) {
            Logger.getLogger(XMLBeanUtils.class.getName()).log(Level.SEVERE, null, ex);
            return objs;
        }
        for (int i = 0; i < objects.getLength(); i++) {
            Element eleObject = (Element) objects.item(i);
            Object obj = createInstance(eleObject,rootPackage,type);
            if (obj != null)
                objs.add(obj);
        }
       
        return objs;
    }
    
    public Object createInstance(Element element, String rootPackage, String type) {
        String className = element.getAttribute("class");
        if (className.isEmpty()) {
            String typeBean = element.getAttribute("type");
            className = rootPackage + "." + type.toLowerCase() + "." + typeBean + type;
        }
        Object obj = null;
        try {
            obj = Class.forName(className).newInstance();
            intializeFromXML(obj,element);
        } catch (InstantiationException ex) {
            Logger.getLogger(XMLBeanUtils.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(XMLBeanUtils.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(XMLBeanUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return obj;
    }

    public void intializeFromXML(Object bean, Element element) {

        NodeList properties = getChildrenbyTagName(element,"Property");
//        try {
//            properties = (NodeList) xpath.evaluate("Property", element,XPathConstants.NODESET);
//        } catch (XPathExpressionException ex) {
//            Logger.getLogger(XMLBeanUtils.class.getName()).log(Level.SEVERE, null, ex);
//            return;
//        }

        for (int i = 0; i < properties.getLength(); i++) {
            Element eleProperty = (Element) properties.item(i);
            String name = eleProperty.getAttribute("name");
            String value = eleProperty.getTextContent();
            try {
                org.apache.commons.beanutils.BeanUtils.setProperty(bean, name, value);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(XMLBeanUtils.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvocationTargetException ex) {
                Logger.getLogger(XMLBeanUtils.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }
}
